//
//  MyOrderDishes.m
//  X-Touch2
//
//  Created by shengchao yang on 12-3-12.
//  Copyright (c) 2012年 foxhis. All rights reserved.
//

#import "MyOrderDishes.h"
#import "Common.h"
#import "Singleton.h"

@implementation MyOrderDishes
@synthesize momLocalMenu;
@synthesize pmCode;
@synthesize psCode;
@synthesize ppCode;
@synthesize ppType;
@synthesize modDescripts;
@synthesize modNumber;
@synthesize modPrice;
@synthesize modUnits;
@synthesize modCook;
@synthesize modPriceUpdate;
@synthesize modTime;

@synthesize modPluPlu;

//=========================================================== 
// - (id)init
//
//=========================================================== 
- (id)init
{
    self = [super init];
    if (self) {
 
    }
    return self;
}


-(void)updateNumber:(NSString *)newNumber {
    NSString * updateSql = [NSString stringWithFormat:@"update MyOrderDishes set modNumber='%@' where momLocalMenu='%@' and ppCode='%@' and modPrice = '%@' and modTime='%@'", newNumber, momLocalMenu, ppCode, modPrice, modTime];
    self.modNumber = newNumber;
    [DataBaseAccess Update:updateSql];
    
    if ([ppType isEqualToString:@"3"]) {
        [AppSession initMyOrderPluPkgs:self.ppCode];//查询套餐明细
        for (MyOrderPluPkgs *tempPkg in AppSession.myOrderPluPkgs) {
            [tempPkg updateNumber:newNumber];
        }
    }
}

-(void)updateCook:(NSString *)newCook {
    NSString * updateSql = [NSString stringWithFormat:@"update MyOrderDishes set modCook='%@' where momLocalMenu='%@' and ppCode='%@' and modPrice = '%@' and modTime='%@'", newCook, momLocalMenu, ppCode, modPrice, modTime];
    self.modCook = newCook;
    [DataBaseAccess Update:updateSql];
    
    if ([ppType isEqualToString:@"3"]) {
        [AppSession initMyOrderPluPkgs:self.ppCode];//查询套餐明细
        for (MyOrderPluPkgs *tempPkg in AppSession.myOrderPluPkgs) {
            [tempPkg updateCook:newCook];
        }
    }
}

-(void)updateName:(NSString *)newName {
    NSString * updateSql = [NSString stringWithFormat:@"update MyOrderDishes set modDescripts='%@' where momLocalMenu='%@' and ppCode='%@' and modPrice = '%@' and modTime='%@'", newName, momLocalMenu, ppCode, modPrice, modTime];
    self.modDescripts = newName;
    [DataBaseAccess Update:updateSql];
}

-(void)updatePrice:(NSString *)newPrice {
    NSString * updateSql = [NSString stringWithFormat:@"update MyOrderDishes set modPrice='%@' where momLocalMenu='%@' and ppCode='%@' and modPrice = '%@' and modTime='%@'", newPrice, momLocalMenu, ppCode, modPrice, modTime];
    self.modPrice = newPrice;
    [DataBaseAccess Update:updateSql];
}

//MyOrderDishes的属性
-(void)updatePluPlu {
    FMDatabase *db =[FMDatabase databaseWithPath:DBRealPath];
    if (![db open]) {
        MyNSLog(@"could not open db");
    }
    
    FMResultSet *rs=[db executeQuery:[NSString stringWithFormat:@"select * from PluPlu where ppCode='%@'", ppCode]];
    PluPlu *temp = [PluPlu new];
    if ([rs next]) {
        temp.pmCode = FormatNilObject([rs stringForColumn:@"pmCode"]);
        temp.psCode = FormatNilObject([rs stringForColumn:@"psCode"]);
        temp.ppCode = FormatNilObject([rs stringForColumn:@"ppCode"]);
        temp.ppTemp = FormatNilObject([rs stringForColumn:@"ppTemp"]);
        temp.ppStatus = FormatNilObject([rs stringForColumn:@"ppStatus"]);
        
        temp.ppColor = FormatNilObject([rs stringForColumn:@"ppColor"]);
        temp.ppPosition = FormatNilObject([rs stringForColumn:@"ppPosition"]);
        temp.ppHelpCode = FormatNilObject([rs stringForColumn:@"ppHelpCode"]);
        temp.ppDescripts = FormatNilObject([rs stringForColumn:@"ppDescripts"]);
        temp.ppImage = FormatNilObject([rs stringForColumn:@"ppImage"]);
        temp.ppRecommended = FormatNilObject([rs stringForColumn:@"ppRecommended"]);
        temp.ppSeqNormal = FormatNilObject([rs stringForColumn:@"ppSeqNormal"]);
        temp.ppSeqCommend = FormatNilObject([rs stringForColumn:@"ppSeqCommend"]);
        temp.ppType = FormatNilObject([rs stringForColumn:@"ppType"]);
        temp.ppFood = nil;
        temp.ppCigarete = nil;
        temp.ppDrink = nil;
    }
    [rs close];
    [db close];    
    
    if ([temp.ppType isEqualToString:@"0"]) {//初始化食品属性
        [temp updatePluFood];
    }else if([temp.ppType isEqualToString:@"1"]){//初始化香烟属性
        [temp updatePluCigarete];
    }else if([temp.ppType isEqualToString:@"2"]){//初始化酒水属性
        [temp updatePluDrink];
    }else if([temp.ppType isEqualToString:@"3"]){//实例化套餐明细数组
        [temp updatePluPkg];
    }    
    self.modPluPlu = temp;
    [temp release];
}

//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
    [momLocalMenu release];
    momLocalMenu = nil;
    [pmCode release];
    pmCode = nil;
    [psCode release];
    psCode = nil;
    [ppCode release];
    ppCode = nil;
    [ppType release];
    ppType = nil;
    [modDescripts release];
    modDescripts = nil;
    [modNumber release];
    modNumber = nil;
    [modPrice release];
    modPrice = nil;
    [modUnits release];
    modUnits = nil;
    [modCook release];
    modCook = nil;
    [modTime release];
    modTime = nil;
    
    [modPluPlu release];
    modPluPlu = nil;
    [modPriceUpdate release];
    modPriceUpdate = nil;
    
    [super dealloc];
}


@end
